﻿(function () {

    layui.use(["form", "layer", "jquery", "upload"], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        var upload = layui.upload;
        var wechatPath = "";
        var alipayPath = "";

        $(document).ready(function () {
            wechatPath = $("#_wechatPath").val();
            alipayPath = $("#_alipayPath").val();

            $("#viewWechatImage").click(onViewWechatImageClick);
            $("#viewAlipayImage").click(onViewAlipayImageClick);

            upload.render({
                elem: "#upload-wechat",
                url: "/UserAccount/UploadImage",
                size: 1000,
                done: function (res, index, upload) {
                    //console.log(res);
                    if (res.Success == true) {
                        wechatPath = res.Data;
                        layer.msg(addStyle("上传成功！"), { icon: 1, shade: 0.01, time: 1 * 1000 });
                    } else {
                        layer.msg(addStyle("上传失败！" + res.Msg), { icon: 2, shade: 0.01, time: 3 * 1000, closeBtn: true });
                    }
                },
                error: function (index, upload) {
                    layer.msg(addStyle("上传失败！" + res.Msg), { icon: 2, shade: 0.01, time: 3 * 1000, closeBtn: true });
                }
            });

            upload.render({
                elem: "#upload-alipay",
                url: "/UserAccount/UploadImage",
                size: 1000,
                done: function (res, index, upload) {
                    //console.log(res);
                    if (res.Success == true) {
                        alipayPath = res.Data;
                        layer.msg(addStyle("上传成功！"), { icon: 1, shade: 0.01, time: 1 * 1000 });
                    } else {
                        layer.msg(addStyle("上传失败！" + res.Msg), { icon: 2, shade: 0.01, time: 3 * 1000, closeBtn: true });
                    }
                },
                error: function (index, upload) {
                    layer.msg(addStyle("上传失败！" + res.Msg), { icon: 2, shade: 0.01, time: 3 * 1000, closeBtn: true });
                }
            });
        });

        form.on("submit(filter-saveAccountInfo)", function (data) {
            //console.log(data);

            var jsonData = {
                BankAccountName: $("#BankAccountName").val(),
                BankName: $("#BankName").val(),
                BankCardNo: $("#BankCardNo").val(),
                WechatPath: wechatPath,
                AlipayPath: alipayPath
            };

            var loadingIndex = layer.msg(addStyle("正在保存账户信息......"), { icon: 16, shade: 0.01, time: false });
            $.ajax("/UserAccount/UpdateAccountInfo", {
                method: "POST",
                data: jsonData,
                complete: function (jqXHR, textStatus) {
                    layer.close(loadingIndex);
                },
                success: function (data, textStatus, jqXHR) {
                    if (data.Success == true) {
                        layer.msg(addStyle("保存成功！"), {
                            icon: 1, time: 1 * 1000, end: function () {
                                
                            }
                        });
                    } else {
                        layer.msg(addStyle("保存失败: " + data.Msg), { icon: 2, time: 3 * 1000, closeBtn: true });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(addStyle("系统错误，请联系管理员！"), { icon: 2, time: 3 * 1000, closeBtn: true });
                }
            });

            return false;
        });

        function showPop(path) {
            var width = $(window).width();
            var height = $(window).height();
            var width2 = width * 0.9;
            var height2 = height * 0.9;

            layer.open({
                type: 1,
                title: false,
                shadeClose: false,
                resize: false,
                area: ["" + width2 + "px", "" + height2 + "px"],
                content: '<div class="bigImage"><img src="' + path + '" style="width: 100%; height: 100%;" /></div>'
            });
        }

        function onViewWechatImageClick() {
            if (wechatPath != "") {
                showPop(wechatPath);
            } else {
                layer.msg(addStyle("未上传微信收款码！"), { icon: 2, time: 3 * 1000, closeBtn: true });
            }
        }

        function onViewAlipayImageClick() {
            if (alipayPath != "") {
                showPop(alipayPath);
            } else {
                layer.msg(addStyle("未上传支付宝收款码！"), { icon: 2, time: 3 * 1000, closeBtn: true });
            }
        }

        function addStyle(str) {
            return '<span style="font-size: 16px;">' + str + '</span>';
        }
    });

})();